using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class CommonVideoPlayManager : Singleton<CommonVideoPlayManager>
    {
        private CommonVideoPlayManager()
        {
        }

        private VideoPlayerForUI _videoPlayer;
        public CanvasGroup rawAlpha { get; private set; }
        private bool playing => this._videoPlayer != null;

        private bool isEditorMode
        {
            get
            {
#if UNITY_EDITOR
                return BattleClientUtil.IsEditorMode || StoryClientUtil.IsEditorMode;
#else
                return false;
#endif
            }
        }

        public void Initialize()
        {
        }

        protected void Dispose()
        {
            if (this.playing)
            {
                this.StopVideo();
                ManagerCenter.UI.CloseWindow<UIVideoPlayer>();
            }
        }

        public VideoPlayerForUI CreatePlayer()
        {
            if (this._videoPlayer == null)
            {
                this._videoPlayer = ManagerCenter.Video.GetVideoPlayerForUI(true);
            }
            var rWindow = ManagerCenter.UI.GetEntityUI<UIVideoPlayer>();
            if (rWindow == null)
            {
                ManagerCenter.UI.ShowWindowSync<UIVideoPlayer>();
                rWindow = ManagerCenter.UI.GetEntityUI<UIVideoPlayer>();
            }
            this._videoPlayer.SetVideoTarget(rWindow.panel);
            this.rawAlpha = rWindow.alphaCtrl;
            this.rawAlpha.alpha = 1;
            return this._videoPlayer;
        }

        public VideoPlayerForUI GetOrCreatePlayer()
        {
            if (this._videoPlayer == null)
            {
                return this.CreatePlayer();
            }
            return this._videoPlayer;
        }

        public async UniTask PlayVideo(string path)
        {
#if UNITY_EDITOR
            if (this.isEditorMode)
            {
                if (StoryEditorMainUpdate.Instance && Application.isPlaying)
                {
                    this._videoPlayer = StoryEditorMainUpdate.Instance.GetOrCreateVideoPlayer();
                    await this._videoPlayer.Play(path);
                }
                else
                {
                    // 启动的预览下才拨片，否则等三秒
                    await UniTask.Delay(3000);
                }
                return;
            }
#endif
            
            this.CreatePlayer();
            if (this._videoPlayer != null)
            {
                await this._videoPlayer.Play(path);
            }
        }

        public void StopVideo()
        {
            if (this._videoPlayer != null)
            {
                // 引用置空前，必须调用这个,否则内存泄露。StopVideo会返回对象池
                this._videoPlayer.StopVideo();
                this._videoPlayer = null;
            }
            
#if UNITY_EDITOR
            if (this.isEditorMode)
            {
                if (StoryEditorMainUpdate.Instance)
                {
                    StoryEditorMainUpdate.Instance.videoPanel.enabled = false;
                }
            }
            else
#endif
            {
                var rWindow = ManagerCenter.UI.GetEntityUI<UIVideoPlayer>();
                if (rWindow != null)
                {
                    rWindow.panel.enabled = false;
                }
            }
            this.rawAlpha = null;
        }
    }
}